<?php
namespace Home\Controller;
use Think\Controller;
use Home\Model\ContactModel;
require_once APP_CLASS.'myRedis.php';
class IndexController extends Controller {
    protected $model;
    protected $redis;
    public function __construct(){
        parent::__construct();
        $this->model=new ContactModel();
        $this->redis=S(array('type'=>'Redis','host'=>'127.0.0.1','port'=>'6379','timeout'=>false));
    }
    public function index(){
        
    }
    /**
     *测试自己封装的redis
     */
    public function redis(){
        $redis=new \myRedis();
        // if($redis->set('key1','1')){
        //     echo 'insert success';
        // }
        //$redis->expire('key1',100);
        echo $redis->ttl('key1');
    }
    /**
     *生成激活码
     */
    public function createActiveCode(){
        set_time_limit(0);
        for($a=0;$a<1000;$a++){
            $code=md5($this->get_random_string().time().'contact');
            $data['code']=$code;
            M('actcode')->add($data);
            flush();
            usleep(100);
            echo $code.'<br>';
        }
    }
    /**
     *获取16位随机字母数字
     */
    private function get_random_string(){
        $array=array(
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            '0','1','2','3','4','5','6','7','8','9'
        );
        $max=count($array)-1;
        for($count=1;$count<=16;$count++){
            $string.=$array[rand(0,$max)];
        }
        return $string;
    }
    public function test(){
        die();
        set_time_limit(0);
        $page='http://m.58.com/fs/';
        $this->getAppAllUrl($page);
    }
    public function getCache(){
        p(S('name'));
    }
    public function geturlmsg(){
        set_time_limit(0);
        //$url = 'http://fs.58.com/jiajuweixiu/?utm_source=market&spm=b-31580022738699-me-f-824.bdpz_biaoti&PGTID=0d30216e-000d-eb62-302a-4a86e7087821&ClickID=1';
        $url ='http://fs.58.com/banjia/?PGTID=0d100000-000d-e7bb-9137-7eb74a610928&ClickID=1';
        $ch = curl_init($url); //初始化会话
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_COOKIE, $this->config_arr['user_cookie']);  //设置请求COOKIE
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
        $result = curl_exec($ch);
        $tbody=substr($result,stripos($result,'<!-- =S infolist -->'),stripos($result,'<!-- =E infolist -->')-stripos($result,"<!-- =S infolist -->"));
        $preg='/<a[^>]+>[^>]+a>/';
        preg_match_all($preg,$tbody,$match);
        foreach ($match[0] as $key => $value) {
            if(strlen($value)<50){
                unset($match[0][$key]);
            }
            else if(strpos($value,'javascript:;')||strpos($value,'class=\'u\'')){
                unset($match[0][$key]);
            }
        }
        $match=$match[0];
        $a=1;
        foreach ($match as $key => $value) {
            $match[$key]=substr($value,stripos($value,'http'),stripos($value,'\' ')-stripos($value,'http'));
            echo $a++.'. ';
            $this->geturlDetail($match[$key]);
        }
        //p($match);
        //echo $result;  //抓取的结果
    }
    public function geturlDetail($url){
        //$url = 'http://fs.58.com/jiajuweixiu/26122094717622x.shtml?spm=b-31580022738699-me-f-824.bdpz_biaoti&utm_source=market&adtype=1&entinfo=26122094717622_0&adact=3&psid=175805286192453517278811377&iuType=q_2&ClickID=9&PGTID=0d30216e-000d-efa7-9f0b-b0905e079869'; //此处mora-hu代表用户ID
        $ch = curl_init($url); //初始化会话
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_COOKIE, $this->config_arr['user_cookie']);  //设置请求COOKIE
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
        $result = curl_exec($ch);
        $h1Preg = '/<h1>(.*?)<\/h1>/is';
        preg_match($h1Preg,$result,$h1str);
        $tbody=substr($result,stripos($result,'<!-- sumary 摘要(帖子列别、用户的联系方式等) -->'),stripos($result,'<!-- 详细描述 -->')-stripos($result,"<!-- sumary 摘要(帖子列别、用户的联系方式等) -->"));
        $liPreg = '/<li>(.*?)<\/li>/is';
        preg_match_all($liPreg,$tbody,$li);
        $li=$li[0];
        $chinesePreg='/[\x{4e00}-\x{9fa5}]+/u';
        preg_match_all($chinesePreg,$li[0],$place);
        preg_match_all($chinesePreg,$li[1],$type);
        preg_match_all($chinesePreg,$li[2],$name);
        $place=$place[0];
        $type=$type[0];
        $name=$name[0];
        $phonePreg = "/1[34578]{1}\d{9}/"; 
        preg_match($phonePreg,$li[3],$phoneNum);
        echo $h1str[1].'<br>';
        foreach ($place as $key => $value) {
            if($key==0)continue;
            echo $value.',';
        }
        echo '<br>';
        echo $type[1].'<br>';
        echo $name[1].'<br>';
        echo $phoneNum[0].'<br>';
        echo '<hr>';
        echo '<br>';
        flush();
        usleep(10);
        // p($h1str);
        // p($place);
        // p($type);
        // p($name);
        // p($phoneNum);
        // p($li);
    }
    /**
     *获取58同城app首页分类相关链接信息
     */
    public function getAppAllUrl($page){
        $host='http://m.58.com';
        $ch = curl_init($page); //初始化会话
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_COOKIE, $this->config_arr['user_cookie']);  //设置请求COOKIE
        //curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
        $result = curl_exec($ch);
        $pattern='/<dl id=\"jzfw\".*<\/dl>/isU';
        preg_match($pattern,$result,$match);
        $pattern1='/<a href=\".*\"/isU';
        preg_match_all($pattern1,$match[0],$match);
        foreach ($match[0] as $key => $value) {
            if($key<2)continue;
            $urlStr=$host.substr($value,strpos($value,'"')+1);
            $subUrl[]=substr($urlStr,0,strlen($urlStr)-1);
        }
        foreach ($subUrl as $key => $value) {
            if($key==0||$key==1)continue;
            $this->getUserDetail($value);
        }
    }
    /**
     *获取一个分类下的所有分页信息
     */
    private function getUserDetail($subUrl){
        for($page=1;$page<=100;$page++){
            $subUrls=$subUrl."pn$page/";
            $ch = curl_init($subUrls); //初始化会话
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_COOKIE, $this->config_arr['user_cookie']);  //设置请求COOKIE
            //curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
            $result = curl_exec($ch);
            if(strpos($result,'infoList')){
                $pattern='/<ul class=\"infoList\">.*<\/ul>/isU';
            }
            else if(strpos($result,'list-info')){
                $pattern='/<ul class=\"list-info\">.*<\/ul>/isU';
            }
            else {
                return;
            }
            preg_match($pattern,$result,$match);
            $pattern1='/<a href=\".*\"/isU';
            preg_match_all($pattern1,$match[0],$match);
            foreach ($match[0] as $key => $value) {
                if(strlen($value)>400)continue;
                $value=substr($value,strpos($value,'http'));
                $itemUrl[]=substr($value,0,strlen($value)-1);
            }
            $testUrl[0]=$itemUrl[0];
			$testUrl[1]=$itemUrl[20];
			$testUrl[2]=$itemUrl[count($itemUrl)-1];
            if($this->analysisDetail($testUrl)){
				$this->analysisDetail($itemUrl);
				curl_close($ch);
			}
			else{
				curl_close($ch);
				continue;
			}
            // $this->analysisDetail($itemUrl);
            // curl_close($ch);
            //return;
        }
    }
    /*
     *解析数据，写入数据库
     */
    private function analysisDetail($detailUrl){
        $count=0;
        foreach ($detailUrl as $key => $value) {
            $ch = curl_init($value); //初始化会话
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_COOKIE, $this->config_arr['user_cookie']);  //设置请求COOKIE
            //curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
            $result = curl_exec($ch);
            $h1Preg = '/<h1 class="tit">(.*?)<\/h1>/is';
            preg_match($h1Preg,$result,$h1str);
            $title=$h1str[1];
            $pattern='/<article class=\"article\">(.*)<\/article>/is';
            preg_match($pattern,$result,$body);
            $patternli='/<li>(.*?)<\/li>/is';
            preg_match_all($patternli,$body[1],$li);
            $li=$li[1];
            $chinesePreg='/[\x{4e00}-\x{9fa5}]+/u';
            if(count($li)==5){
                preg_match_all($chinesePreg,$li[0],$type);
                preg_match_all($chinesePreg,$li[1],$place);
                preg_match_all($chinesePreg,$li[2],$name);
                preg_match('/1[34578]{1}\d{9}/',$li[3],$phone);
                foreach ($place[0] as $key => $values) {
                    if($key===0)continue;
                    $places.=$values;
                }
            }
            else{
                preg_match_all($chinesePreg,$li[0],$type);
                preg_match_all($chinesePreg,$li[1],$name);
                preg_match('/1[34578]{1}\d{9}/',$li[2],$phone);
            }
            echo $value."\n ";
            echo $title."\n ";
            echo $name[0][3]."\n ";
            echo $phone[0]."\n ";
            echo $type[0][1]."\n ";
            echo $places."\n ";
            $data['title']=$title;
            $data['name']=$name[0][3];
            $data['phone_num']=$phone[0];
            $data['address']=$places;
            $data['type']=$type[0][1];
            if(empty($phone[0])||empty($name[0][3])){
                continue;
            }
            if($this->model->Insert($data)){
                $count++;
                echo 'true<br>';
            }
            echo "----------------------------------------------\n";
            $places='';
            flush();
            usleep(10);
        }
    }
}